import pandas as pd

def col_sum():
    df1 = pd.DataFrame(
        [{"r1": 1, "r2": 1, "symbol": 2},
         {"r1": 2, "r2": 2, "symbol": 3},
         {"r1": 1, "r2": 2, "symbol": 4},
         {"r1": 1, "r2": 3, "symbol": 2}
         ]
    )
    print(df1)
    df1["sum"] = df1.sum(axis=1)
    print(df1)


def col_not_zero_count():
    df1 = pd.DataFrame(
        [{"r1": 1, "r2": 0, "symbol": -2},
         {"r1": 0, "r2": 0},
         {"r1": 1, "r2": 2, "symbol": 4},
         {"r1": 1, "r2": 3, "symbol": 2}
         ]
    )
    #print((df1.fillna(0) != 0).astype(int).sum(axis=1))
    print((df1 > 0).astype(int).sum(axis=1))
    print(df1)


def nan_value_computer():
    # 测试nan值的运算是否仍为nan
    df1 = pd.DataFrame(
        [{"r1": 1, "r2": 0, "symbol": -2},
         {"r1": 0, "r2": 0},
         {"r1": 1, "r2": 2, "symbol": 4},
         {"r1": 1, "r2": 3, "symbol": 2}
         ]
    )
    print(df1["r1"]*df1["symbol"])
    print(df1)


if __name__ == '__main__':
    #col_not_zero_count()
    nan_value_computer()
